import operator
from typing import Annotated,List,TypedDict

from langgraph.graph import StateGraph,START,END

class State(TypedDict):
    messages:Annotated[List[str],operator.add]

def addition(state):
    print(state)
    message = state["messages"][-1]
    response = {"x":message["x"]+1}
    return {"messages":[response]}

def substraction(state):
    print(state)
    message = state["messages"][-1]
    response = {"x":message["x"]-2}
    return {"messages":[response]}

builder = StateGraph(State)


builder.add_node("addition",addition)
builder.add_node("substraction",substraction)

builder.add_edge(START,"addition")
builder.add_edge("addition","substraction")
builder.add_edge("substraction",END)

graph = builder.compile()
resp = graph.invoke({"messages":[{"x":10}]})
print(resp)
